﻿var allDepData = "";
$(function () {
    $.ajax({
        type: "GET",
        url: "/Dzb/Party/GetAllListToJson?_t=" + new Date().getTime(),
        async: false,
        success: function (data) {
            allDepData = data;
            loadParty();
        }
    });

    $("#btnAddRoot").click(function () { add(0); });
    $("#btnAdd").click(function () { add(1); });
    $("#btnSave").click(function () { save(); });
    $("#btnDelete").click(function () { deleteMulti(); });    
    $("#checkAll").click(function () { checkAll(this) });
    initTree();
    loadTables(1);
});
//加载功能树
function initTree() {
    $.jstree.destroy();
    $.ajax({
        type: "Get",
        url: "/Dzb/Party/GetTreeData?_t=" + new Date().getTime(),    //获取数据的ajax请求地址
        success: function (data) {
            $('#treeDiv').jstree({       //创建JsTtree
                'core': {
                    'data': data,        //绑定JsTree数据（必须包括三个字段：id,parent,text)
                    "multiple": false    //是否多选
                },
                "plugins": ["state", "types", "wholerow"]  //配置信息
            })
            $("#treeDiv").on("ready.jstree", function (e, data) {   //树创建完成事件
                data.instance.open_all();    //展开所有节点
                loadTables(0, 1);
            });
            $("#treeDiv").on('changed.jstree', function (e, data) {   //选中节点改变事件
                var node = data.instance.get_node(data.selected[0]);  //获取选中的节点
                if (node) {
                    selectedMenuId = node.id;
                    loadTables(selectedMenuId,1);
                };
            });
        }
    });

}
//加载功能列表数据
function loadTables(partyId,pageIndex) {
    $("#tableBody").html("");
    $("#checkAll").prop("checked", false);
    $.ajax({
        type: "GET",
        url: "/Dzb/PartyMember/GetPagedListByPartyId?partyId=" + partyId + "&pageindex=" + pageIndex + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data.rows, function (i, item) {
                var tr = "<tr>";
                tr += "<td align='center'><input type='checkbox' class='checkboxs' value='" + item.id + "'/></td>";
                tr += "<td>" + item.name + "</td>";
                tr += "<td>" + item.position + "</td>";
                tr += "<td>" + findPartyNameByPartyId(item.partyId) + "</td>";
                tr += "<td>" + item.tel + "</td>";                
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='edit(\"" + item.id + "\")'><i class='fa fa-edit'></i> 编辑 </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingle(\"" + item.id + "\")'><i class='fa fa-trash-o'></i> 删除 </button> </td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            })
            var elment = $("#grid_paging_part"); //分页插件的容器id
            if (data.rowCount > 0) {
                var options = { //分页插件配置项
                    bootstrapMajorVersion: 3,
                    currentPage: pageIndex, //当前页
                    numberOfPages: data.rowCount, //总数
                    totalPages: data.pageCount, //总页数
                    onPageChanged: function (event, oldPage, newPage) { //页面切换事件
                        loadTables(newPage);
                    },
                    itemTexts: function (type, page, current) {
                        switch (type) {
                            case "first": return "首页";
                            case "prev": return "上一页";
                            case "next": return "下一页";
                            case "last": return "末页";
                            case "page": return page;
                        }
                    }
                }
                elment.bootstrapPaginator(options); //分页插件初始化
            }
        }
    })
}

function loadParty() {
    $("#PartyId").html("");
    var option = "";

    $.each(allDepData, function (i, item) {
        if (item.parentId == 13) {
            option += "<option value='" + item.id + "'>" + item.name + "</option>"
        }
    })
    $("#PartyId").append(option);
}

//全选
function checkAll(obj) {
    $(".checkboxs").each(function () {
        if (obj.checked == true) {
            $(this).prop("checked", true)

        }
        if (obj.checked == false) {
            $(this).prop("checked", false)
        }
    });
};

function findPartyNameByPartyId(partyId) {
    var html = "";
    $.each(allDepData, function (i, item) {
        if (item.id == partyId) {
            html = item.name;
            //alert(html);
            return html;
        }
    });
    return html;
}

//新增
function add(type) {    
    $("#Id").val(0);
    $("#Name").val("");
    $("#Position").val("");    
    $("#Tel").val(0);
    $("#PicPath").val("");
    $("#PartyId").val("");
    $("#IsGood").val("");
    $("#SortIndex").val(0);
    $("#Remarks").val("");

    $("#Title").text("新增");
    //弹出新增窗体
    $("#addRootModal").modal("show");
};
//编辑
function edit(id) {
    $.ajax({
        type: "Get",
        url: "/Dzb/PartyMember/GetByIdToJson?id=" + id + "&_t=" + new Date(),
        success: function (data) {
            $("#Id").val(data.id);
            $("#SortIndex").val(data.sortIndex);
            $("#Remarks").val(data.remarks);
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);
            
            $("#Name").val(data.name);
            $("#Position").val(data.position);
            $("#Tel").val(data.tel);
            $("#PicPath").val(data.picPath);
            $("#PartyId").val(data.partyId);            
            $("#IsGood").val(data.isGood);            

            $("#Title").text("编辑功能")
            $("#addRootModal").modal("show");
        }
    })
};
//保存
function save() {
    var postData = {
        "Id": $("#Id").val(),        
        "Name": $("#Name").val(),
        "Position": $("#Position").val(),
        "Tel": $("#Tel").val(),
        "PicPath": $("#PicPath").val(),
        "PartyId": $("#PartyId").val(),
        "IsGood": $("#IsGood").val(),
        "SortIndex": $("#SortIndex").val(),
        "Remarks": $("#Remarks").val(),
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val()
    };
    $.ajax({
        type: "Post",
        url: "/Dzb/PartyMember/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadTables(1)
                $("#addRootModal").modal("hide");
            } else {
                layer.tips(data.message, "#btnSave");
            };
        }
    });
};
//批量删除
function deleteMulti() {
    var ids = "";
    $(".checkboxs").each(function () {
        if ($(this).prop("checked") == true) {
            ids += $(this).val() + ","
        }
    });
    ids = ids.substring(0, ids.length - 1);
    if (ids.length == 0) {
        layer.alert("请选择要删除的记录。");
        return;
    };
    //询问框
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        var sendData = { "ids": ids };
        $.ajax({
            type: "Post",
            url: "/Dzb/PartyMember/DeleteMutiByIds",
            data: sendData,
            success: function (data) {
                if (data.result == "Success") {
                    initTree();
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        });
    });
};
//删除单条数据
function deleteSingle(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Dzb/PartyMember/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    initTree();
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        })
    });
};


